package com.example.shop_2212.fgviewmodel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.example.base.entity.*
import com.example.base.mvvm.BaseViewModel
import com.umeng.commonsdk.debug.E
import kotlinx.coroutines.launch

class CarViewModel:BaseViewModel() {
    //返回值---获取购物车列表
    val carSuccess = MutableLiveData<CarEntityResult>()
    val carFail = MutableLiveData<String>()
    fun car(isValid:String,limit:String,page:String){
        viewModelScope.launch {
            try {
                carSuccess.value = apiServer.car(isValid, limit, page)
            }catch (e:Exception){
                carFail.value = e.message
            }
        }
    }
    //返回值--修改购物车数量
    val updateSuccess = MutableLiveData<UpdateEntityResult>()
    val updateFail = MutableLiveData<String>()
    fun update(id:Int,number:Int){
        viewModelScope.launch {
            try {
                updateSuccess.value = apiServer.update(id, number)
            }catch (e:Exception){
                updateFail.value = e.message
            }
        }
    }
    //返回值--删除购物车
    val deleteSuccess = MutableLiveData<DeleteEntityResult>()
    val deleteFail = MutableLiveData<String>()
    fun delete(ids:Array<Int>){
        viewModelScope.launch {
            try {
                deleteSuccess.value = apiServer.delete(ids)
            }catch (e:Exception){
                deleteFail.value = e.message
            }
        }
    }
    //返回值--预下单
    val preOrderSuccess = MutableLiveData<PreOrderEntityResult>()
    val preOrderFail = MutableLiveData<String>()
    fun preOrder(body:ParamPreOrder){
        viewModelScope.launch {
            try {
                preOrderSuccess.value = apiServer.preOrder(body)
            }catch (e:Exception){
                preOrderFail.value = e.message
            }
        }
    }

}